<?php
namespace Weibo\Controller;
use Weibo\Controller\WeibobaseController;

/**
 * 微博控制器
 * @author Baiyu 2016-12-17
 */
class WeiboController extends WeibobaseController {
	
	/**
	 * 获取微博列表
	 * @author	Baiyu
	 * @param	array|string $param		输入的各种条件
	 */
	public function index($param = ''){
		
		//每次刷新得到多少条微博
		$items_per_refresh = 20;
		
		//参数范例
		$param_sample = array(
				'followed' => FALSE,					//是否关注的人发表的
				'friends' => FALSE,						//是否相互关注的人发表的
				'author' => 1,							//作者uid,可实现我的微博和用户微博列表
				'time' => '1481963314,1481966666',		//时间戳或时间戳的区间
				'hot' => FALSE,							//是否获取热门微博
				'city' => 1,							//城市ID
				'location' => 1,						//定位ID
				'nearby' => 4,							//附近4百米内的微博,需要计算4个象限
				'orderby' => 'time',					//全部是desc的,time时间,hot热力值,forward转发次数,comment评论次数,praise赞次数
		);
		
		/*
		 * STEP1 申明变量,重要的且肯定会用到的变量
		 */
		//$param = array();					//输入参数
		$error = FALSE;						//错误代码
		$error_msg = '';					//错误信息
		$where = array();					//查询微博列表的条件
		$weibo_ids = array();				//微博列表的ID数组
		$original_weibo_ids = array();		//转发的原始微博ID数组
		$all_weibo_ids = array();			//所有微博ID包括列表的微博和被转发的微博的ID
		$user_ids = array();				//微博列表涉及到的用户ID数组
		$user_list = array();				//相关用户列表数据(用户模块)
		$weibo_user_list = array();			//微博用户列表数据(微博模块)
		$weibo_list = array();				//最终返回的微博列表
		$weibo_id_listkey_index = array();	//weibo_id对应weibo_list键的数组,方便从weibo_list中按weibo_id取出数据
		$has_more = FALSE;					//本次获取的列表与上次获取之间是否有漏掉的数据
		$order_array = array();				//用于排序算法的临时数组,排序依据字段和weibo_id的对应关系数组
		$order_by_key = '';					//用于排序算法中储存排序依据字段的名称
		$order_by_value = '';				//用于排序算法中储存排序方法,如DESC 或一个数组,代表按这个数组的值的顺序进行排序
		$order_weibo_list = array();		//用于排序算法的临时微博列表
		$at_nicknames = array();			//列表中全部@的用户nickname
		$weibo_at_name_id_array = array();	//列表中所有@的用户的nickname与user_id对应关系二维数组,一维的键为weibo_id便于整理数据时整合进weibo_list
		
		
		/*
		 * STEP2 组织参数
		 */
		//STEP2.1 默认参数
		$default_param = array(
				'orderby' => 'create_time',
		);
		
		//STEP2.2 可以接收base64编码的param参数
		if (is_string($param)) {
			$param = base64_decode($param);
			$param && $param = json_decode($param, TRUE);
			
		}
		!is_array($param) && $param = array();
		
		//STEP2.3 合并param与默认参数
		$param = array_merge($default_param, $param);
		
		//STEP2.4 也可以接收直接GET的参数
		isset($param['followed'])	or $param['followed']	= I('followed');
		isset($param['friends'])	or $param['friends']	= I('friends');
		isset($param['author'])		or $param['author']		= I('author');
		isset($param['city'])		or $param['city']		= I('city');
		isset($param['location'])	or $param['location']	= I('location');
		isset($param['nearby'])		or $param['nearby']		= I('nearby');
		isset($param['hot'])		or $param['hot']		= I('hot');

		isset($param['time'])		or $param['time']		= I('time');
		isset($param['orderby'])	or $param['orderby']	= I('orderby');
		
		/*
		 * STEP3 组织查询条件
		 */
		//STEP3.1 获取起止时间
		if ($param['time']) {
			if (strpos($param['time'], ',') !== FALSE) {
				list($param['time_begin'], $param['time_end']) = explode(',', $param['time']);
			} else {
				$param['time_begin'] = $param['time'];
			}
			empty($param['time_end']) && $param['time_end'] = NOW_TIME;
			$param['where_create_time'] = array('BETWEEN', array(intval($param['time_begin']), intval($param['time_end'])));
		}
		
		//STEP3.2 主要条件,关注的人的微博 | 某一个用户的微博 | 某城市的微博 | 某地点的微博 | 附近的微博
		$where = array();
		//关注的人的微博
		if ($param['followed']) {
			
			//查关注的user_ids
			$followed = D('WeiboUserRelation') -> getFollowed($this -> userId);
			$followed[] = $this -> userId;	//列表中也要显示自己的微博
			
			//查weibo_ids
			$index_feed_where['author_id'] = array('IN', $followed);
			$param['where_create_time'] && $index_feed_where['create_time'] = $param['where_create_time'];
			!empty($followed) && $weibo_ids = D('WeiboIndexFeed') -> where($index_feed_where) -> order(array('create_time' => 'DESC')) -> limit(0, $items_per_refresh + 1) -> getField('weibo_id', TRUE);
			
			if (!empty($weibo_ids)) {
				
				//之所以查weibo_ids时要比刷新的条目数(N)多1，是因为刷新最新微博时如果只取N条可能会有更早的微博没有被获取到，比如上次刷新后到现在总共有100条微博而现在刷新只取20条那么就有80条漏掉了。
				if (count($weibo_ids) > $items_per_refresh) {
					array_pop($weibo_ids);
					$has_more = 1;
				}
				
				//要获取的微博ID数组
				$where['weibo_id'] = array('IN', $weibo_ids);
			}
		
		//相互关注的人的微博
		} elseif ($param['friends']) {
			
			
			
		//某用户的微博
		} elseif ($param['author']) {
			
			//TODO 将来实现权限机制
			
			//列表中相关用户数组，用于获取用户数据，这里因为作者只有一个所以容易得到
			$user_ids = array($param['author']);
			
			//查weibo_ids

			$index_feed_where['author_id'] = $param['author'];
			$param['where_create_time'] && $index_feed_where['create_time'] = $param['where_create_time'];
			$weibo_ids = D('WeiboIndexFeed') -> where($index_feed_where) -> order(array('create_time' => 'DESC')) -> limit(0, $items_per_refresh + 1) -> getField('weibo_id', TRUE);
			
			
			if (!empty($weibo_ids)) {
			
				//之所以查weibo_ids时要比刷新的条目数(N)多1，是因为刷新最新微博时如果只取N条可能会有更早的微博没有被获取到，比如上次刷新后到现在总共有100条微博而现在刷新只取20条那么就有80条漏掉了。
				if (count($weibo_ids) > $items_per_refresh) {
					array_pop($weibo_ids);
					$has_more = 1;
				}
			
				//要获取的微博ID数组
				$where['weibo_id'] = array('IN', $weibo_ids);
			}
			
		//获取所有热门微博
		} elseif ($param['hot']) {
			
			$weibo_ids = D('WeiboHot') -> order(array('orderby' => 'ASC')) -> limit(0, 100) -> getField('weibo_id', TRUE);
			if (!empty($weibo_ids)) {
				$where['weibo_id'] = array('IN', $weibo_ids);
				$param['orderby'] = array('weibo_id' => $weibo_ids);
			}
			
		} else {
			//TODO 实现city, location, nearby的查询条件
		}
		
		/*
		 * STEP4 查询与整合数据
		 */
		//STEP4.1 查询微博列表
		$weibo_list = array();
		if (!empty($where)) {
			
			D('Weibo') -> where($where);
			
			/*
			if ($param['orderby']) {
				//TODO 只查询,不order,自己实现order
				D('Weibo') -> order(array($param['orderby'] => 'DESC'));
			}*/
			
			$weibo_list = D('Weibo') -> select();
		}
		
		//STEP4.2 整合微博列表相关数据
		if (!empty($weibo_list)) {
			//STEP4.2.1 微博列表中提取微博ID、原始微博ID、用户ID、初始化排序算法、weibo_id对应weibo_list键的数组
			$weibo_id_listkey_index = array();
			$need_weibo_ids = FALSE;
			$need_user_ids = FALSE;
			$need_order_by = FALSE;
			empty($weibo_ids) && $need_weibo_ids = TRUE;
			empty($user_ids) && $need_user_ids = TRUE;
			if (!empty($param['orderby'])) {
				$need_order_by = TRUE;
				if (is_array($param['orderby'])) {
					$order_by_key = key($param['orderby']);
					$order_by_value = reset($param['orderby']);
				} else {
					$order_by_key = $param['orderby'];
					$order_by_value = 'DESC';
				}
				
			}
			foreach ($weibo_list as $k => $v){
				$need_weibo_ids && $weibo_ids[] = $v['weibo_id'];
				$need_user_ids && $user_ids[] = $v['user_id'];
				$need_order_by && $order_array[$v['weibo_id']] = $v[$order_by_key];
				$v['original_user_id'] && $user_ids[] = $v['original_user_id'];
				$v['original_id'] && $original_weibo_ids[] = $v['original_id'];
				
				//@的用户昵称
				$at_nicknames = array_merge($at_nicknames, match_at($v['content']));
				
				//weibo_id与weibo_list的键的对应关系数组,用于排序算法
				$weibo_id_listkey_index[$v['weibo_id']] = $k;
			}
			!empty($user_ids) && $user_ids = array_unique($user_ids);
			!empty($original_weibo_ids) && $original_weibo_ids = array_unique($original_weibo_ids);
			!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			
			//STEP4.2.1微博列表排序
			if (!empty($order_array)) {
				$order_by_value = reset($param['orderby']);
				if (is_array($order_by_value)) {
					$order_by_value = array_flip($order_by_value);
					uasort($order_array, function($a, $b){
						strcasecmp($order_by_value[$a], $order_by_value[$b]);
					});
				} else {
					arsort($order_array);
				}
				
				foreach ($order_array as $order_array_key => $order_array_item) {
					$order_weibo_list[] = $weibo_list[$weibo_id_listkey_index[$order_array_key]];
					unset($weibo_list[$weibo_id_listkey_index[$order_array_key]]);
				}
				$weibo_list = &$order_weibo_list;
			}
			
			//STEP4.2.3 查询微博图片和附件
			$all_weibo_ids = array_unique(array_merge($weibo_ids, $original_weibo_ids));
			if (!empty($all_weibo_ids)) {
				$attachments = D('WeiboAttachment') -> where(array('weibo_id' => array('IN', $all_weibo_ids))) -> select();
				$attachments = field_as_key($attachments, 'weibo_id', TRUE);
			}
			
			//STEP4.2.4 查询相关user信息
			if (!empty($user_ids)) {
				$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
				$weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
				foreach ($weibo_user_list as $weibo_user_list_k => $weibo_user_list_v) {
					$weibo_user_list[$weibo_user_list_k] = array_merge($user_list[$weibo_user_list_k], $weibo_user_list_v);
				}
			}
			
			//STEP4.2.5 查询转发中原始微博列表
			if (!empty($original_weibo_ids)) {
				$original_weibo_list = D('Weibo') -> where(array('weibo_id' => array('IN', $original_weibo_ids))) -> select();
				$original_weibo_list = field_as_key($original_weibo_list, 'weibo_id');
				
				//取得at用户的昵称 并 整合附件数据
				foreach ($original_weibo_list as $original_weibo_k => $original_weibo_v) {
					$original_weibo_list[$original_weibo_k]['time_str'] = time2str($original_weibo_v['create_time']);
					$original_weibo_list[$original_weibo_k]['attach'] = isset($attachments[$original_weibo_v['weibo_id']]) ? $attachments[$original_weibo_v['weibo_id']] : array();
					$original_weibo_list[$original_weibo_k]['user_data'] = $weibo_user_list[$original_weibo_v['user_id']];
					
					$at_nicknames = array_merge($at_nicknames, match_at($original_weibo_v['content']));
				}
				!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			}
			
			//STEP4.2.6查询at的用户的user_ids
			if (!empty($at_nicknames)) {
				$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
				$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
			}
			
			//STEP4.2.7 整合数据
			foreach ($weibo_list as $k => $v){
				$weibo_list[$k]['time_str'] = time2str($v['create_time']);
				$weibo_list[$k]['attach'] = isset($attachments[$v['weibo_id']]) ? $attachments[$v['weibo_id']] : array();
				$weibo_list[$k]['user_data'] = $weibo_user_list[$v['user_id']];
				$weibo_list[$k]['original_weibo'] = $v['original_id'] ? $original_weibo_list[$v['original_id']] : array();
			}
		} else {
			$error = 101;
			$error_msg = '没有更多数据';
		}
		
		/*
		 * 输出结果
		 */
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['list'] = $weibo_list;
		$assign['at_data'] = $weibo_at_name_id_array;
		$assign['has_more'] = intval($has_more);
		$this -> output($assign);
	}
	
	//TODO 怎么从APP传附件
	//发微博时APP先把附件上传并插入attachment表，weibo_id留空
	//发微博时把attachment_id传上去，微博发表成功后update附件表，将weibo_id填入
	//定期清理没有weibo_id的附件
	
	/**
	 * 发微博和转发
	 * //TODO转发机制更新,是否有未写入original_user的bug
	 * @author	Baiyu
	 * @param	string	$content		微博内容
	 * @param	number	$original_id	原微博ID(不为0时为转发)
	 * @param	string	$attach_ids		附件IDs
	 */
	public function add($content = '', $original_id = 0, $attach_ids = ''){
		
		//检测权限是否已停用
		$this -> checkWeiboPermission();
		
		//申明变量
		$result = FALSE;								//操作结果
		$error = FALSE;									//错误代码
		$error_msg = '';								//错误信息
		$log_array = array();							//错误日志数据
		$weibo = array();								//微博数据
		$weibo_id = 0;									//微博ID
		$original_weibo = array();						//原微博(转发时)
		$at_nicknames = array();						//微博中@的用户名
		$at_user_id_name = array();						//被@的用户的ID和昵称对应关系
		$at_user_ids = array();							//微博中@的用户的ID
		$ats = array();									//要插入at表的记录
		$items_per_page = C('WEIBO.page_length');		//每页放多少条转发(转发时)
		$page_index = 0;								//转发所在页码(转发时)
		
		//组织参数
		$attach_ids && !is_numeric($attach_ids) && $attach_ids = strpos($attach_ids, '[') === FALSE && strpos($attach_ids, ',') !== FALSE ? explode(',', $attach_ids) : json_decode($attach_ids);
		$content = substr($content, 0, 140);	//最多允许140字
		
		//匹配@
		if (!empty($content)) {
			$at_nicknames = match_at($content);
			$at_user_id_name = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
			$at_user_ids = array_keys($at_user_id_name);
		}
		
		//TODO敏感词匹配
		
		//查出原微博
		$original_id && $original_weibo = D('Weibo') -> where(array('weibo_id' => $original_id)) -> field(array('original_id', 'user_id', 'original_user_id', 'type', 'forwarded_count')) -> find();
		isset($original_weibo['forwarded_count']) && $page_index = ceil($original_weibo['forwarded_count'] / $items_per_page) ?: 1;
		
		//插入微博表
		if (!empty($original_weibo) || $content) {
			//新微博数据
			$weibo = array(
					'original_id' => 0,
					'user_id' => $this -> userId,
					'original_user_id' => 0,
					'content' => $content,
					'type' => $original_id ? 2 : 1,
					'commented_count' => 0,
					'comment_count' => 0,
					'forwarded_count' => 0,
					'forward_count' => 0,
					'praised_count' => 0,
					'praise_count' => 0,
					'create_time' => NOW_TIME,
					'page_index' => $page_index,
			);
			
			//转发时处理
			if (!empty($original_weibo)) {
				//得到最原始的微博ID和用户ID
				if ($original_weibo['type'] == 2) {
					$weibo['original_id'] = $original_weibo['original_id'];
					$weibo['original_user_id'] = $original_weibo['original_user_id'];
				} elseif ($original_weibo['type'] == 1) {
					$weibo['original_id'] = $original_id;
					$weibo['original_user_id'] = $original_weibo['user_id'];
				}
				
				//转发时只能发一张图片
				$attach_ids = array_slice($attach_ids, 0, 1);
			}
			
			$weibo_id = D('Weibo') -> add($weibo);
		} else {
			//原微博不存在 且 未发表任何内容
			$error = 101;
			$error_msg = '操作失败';
		}
		
		//微博相关数据操作
		if ($weibo_id) {
			
			//插入微博feed表
			$result = TRUE && D('WeiboIndexFeed') -> add(array(
					'weibo_id' => $weibo_id,
					'author_id' => $this -> userId,
					'create_time' => NOW_TIME
			));
			if (!$result) {
				$rollback = D('Weibo') -> delete($weibo_id);
				$log_array['step'] = '8.1';
				$log_array['rollback'] = $rollback ? 'success' : 'failed';
				$log_array['err_msg'] = '插入微博feed表失败';
			}
			
			//修改attachment表中的weibo_id
			$result && $attach_ids && $result = TRUE && D('WeiboAttachment') -> where(array('attachment_id' => array('IN', $attach_ids))) -> save(array('weibo_id' => $weibo_id));
			if (!$result) {
				$log_array['step'] = '8.2';
				$log_array['rollback'] = 'notrun';
				$log_array['err_msg'] = '修改Weibo附件的weibo_id失败';
				$log_array['attach_ids'] = implode('|', $attach_ids);
			}
			
			//修改weibo_user的weibo_count
			$result && $result = TRUE && D('WeiboUser') -> where(array('user_id' => $this -> userId)) -> save(array('weibo_count' => array('exp', 'weibo_count+1')));
			if (!$result) {
				$log_array['step'] = '8.3';
				$log_array['rollback'] = 'notrun';
				$log_array['err_msg'] = '修改作者微博数失败';
			}
			
			//处理转发相关事件
			if (!empty($original_weibo)) {
				
				//插入转发表
				$result && $result = TRUE && D('WeiboForward') -> add(array(
					'weibo_id' => $original_id,
					'new_id' => $weibo_id,
					'original_id' => $weibo['original_id'],		//注意区分两个original,这里这个是指最原始的微博，也就是最初原创的那个而不是被转发的那条微博
					'create_time' => NOW_TIME,
					'page_index' => $page_index,
				));
				if (!$result) {
					$log_array['step'] = '8.4.1';
					$log_array['rollback'] = 'notrun';
					$log_array['err_msg'] = '插入转发记录表时出错';
				}
				
				//修改原微博的转发数
				$result && $result = TRUE && D('Weibo') -> where(array('weibo_id' => $original_id)) -> save(array('forwarded_count' => array('exp', 'forwarded_count+1'), 'forward_count' => array('exp', 'forward_count+1')));
				if (!$result) {
					$log_array['step'] = '8.4.2';
					$log_array['rollback'] = 'notrun';
					$log_array['err_msg'] = '修改原微博的转发数失败';
				}
				
			}
			
			//插入at表(at有可能转发时at也有可以原创微博中就at,所以不放到转发处理中)
			foreach ($at_user_id_name as $k => $v) {
				$ats[] = array(
						'weibo_id' => $weibo_id,
						'original_id' => $original_id,
						'comment_id' => 0,
						'original_comment_id' => 0,
						'type' => 1,
						'from_uid' => $this -> userId,
						'to_uid' => $k,
						'to_nickname' => $v,
						'create_time' => NOW_TIME,
				);
			}
			$result && !empty($ats) && $result = TRUE && D('WeiboAt') -> addAll($ats);
			if (!$result) {
				$log_array['step'] = '8.5';
				$log_array['rollback'] = 'notrun';
				$log_array['err_msg'] = '插入@表时出错';
			}
			
			//@通知
			$result && $at_user_ids && push_weibo_notify($at_user_ids, $this -> userId, 'at');
			
			//错误处理
			if (!$result) {
				$error = 101;
				$error_msg = '操作失败';
			
				$log_array['on'] = $original_id ? 'foward_weibo' : 'add_weibo';
				$log_array['weibo_id'] = $weibo_id;
				$log_array['original_id'] = $original_id;
				$log_array['uid'] = $this -> userId;
				$log_array['step'] = $log_array['step'] ?: '8';
				$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
				weibo_log($log_array);
			}
		}
		
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['status'] = intval($result);
		$weibo_id && $assign['weibo_id'] = $weibo_id;
		$this -> output($assign);
		
		//风险记录
		//TODO
	}
	
	/**
	 * 转发微博
	 * @param string $content
	 * @param number $original_id
	 * @param string $attach_ids
	 */
	public function forward($content = '', $original_id = 0, $attach_ids = ''){
		$this -> add($content, $original_id, $attach_ids);
	}
	
	/**
	 * 微博详情
	 * @author	Baiyu
	 * @param	number	$weibo_id
	 */
	public function detail($weibo_id = 0){
		
		//TODO 将来实现权限机制
		
		//申明变量
		$error = 0;
		$error_msg = '';
		$weibo = array();
		$original_weibo = array();
		$weibo_ids = array();
		$user_ids = array();
		$user_list = array();
		$weibo_user_list = array();
		$attachments = array();
		$weibo_at_name_id_array = array();
		
		//查出微博
		$weibo = D('Weibo') -> find($weibo_id);
		
		if (!empty($weibo)) {
			
			//转发的微博
			if ($weibo['type'] == 2) {
				//整理数据
				$weibo_ids = array($weibo['weibo_id'], $weibo['original_id']);
				$user_ids = array($weibo['user_id'], $weibo['original_user_id']);
				
				//查出原始微博
				$original_weibo = D('Weibo') -> find($weibo['original_id']);
				
			//原创微博
			} else {
				//整理数据
				$weibo_ids = array($weibo['weibo_id']);
				$user_ids = array($weibo['user_id']);
			}
			
			//查出附件
			$attachments = D('WeiboAttachment') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
			$attachments = field_as_key($attachments, 'weibo_id', TRUE);
			
			//匹配@的用户数据
			$at_nicknames = match_at($weibo['content'] . (!empty($original_weibo) ? ' ' . $original_weibo['content'] : ''));
			!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			if (!empty($at_nicknames)) {
				$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
				$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
			}
			
			//查出用户
			$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
			$weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
			foreach ($weibo_user_list as $k => $v) {
				$weibo_user_list[$k] = array_merge($user_list[$k], $v);
			}
			
			//整合数据
			if (!empty($original_weibo)) {
				$original_weibo['time_str'] = time2str($original_weibo['create_time']);
				$original_weibo['attach'] = isset($attachments[$original_weibo['weibo_id']]) ? $attachments[$original_weibo['weibo_id']] : array();
				$original_weibo['user_data'] = $weibo_user_list[$original_weibo['user_id']];
			}
			$weibo['time_str'] = time2str($weibo['create_time']);
			$weibo['attach'] = isset($attachments[$weibo['weibo_id']]) ? $attachments[$weibo['weibo_id']] : array();
			$weibo['user_data'] = $weibo_user_list[$weibo['user_id']];
			$weibo['original_weibo'] = $original_weibo;
			
		} else {
			$error = 102;
			$error_msg = '没有这条微博';
		}
		
		$assign['error'] = $error;
		$assign['error_msg'] = $error_msg;
		$assign['weibo'] = $weibo;
		$assign['at_data'] = $weibo_at_name_id_array;
		$this -> output($assign);
	}
	
	/**
	 * 获取微博的评论列表
	 * @author	Baiyu
	 * @param	number	$weibo_id		微博ID
	 * @param	number	$page			页码
	 * @param	number	$offset			起始行
	 * @param	number	$limit			取几行
	 */
	public function commentList($weibo_id = 0, $page = 0, $offset = 0, $limit = 50){
		
		//TODO 将来实现权限机制
		
		//申明变量
		$limit > 200 && $limit = 200;
		$comment_model = D('WeiboComment');
		$where = array('weibo_id' => $weibo_id);
		$user_ids = array();
		$user_list = array();
		$weibo_user_list = array();
		$comment_list = array();
		$at_nicknames = array();
		$weibo_at_name_id_array = array();
		
		//分页机制 page和offset limit二选一
		if ($page) {
			$where['page_index'] = $page;
		} else {
			$comment_model -> limit($offset, $limit);
		}
		
		//获取评论列表
		$comment_list = $comment_model	-> where($where)
										-> order(array('create_time'=>'DESC'))	//TODO 自己实现order机制
										-> select();
		
		foreach ($comment_list as $item){
			$user_ids[] = $item['from_uid'];
			$user_ids[] = $item['to_uid'];
				
			//@的用户昵称
			$at_nicknames = array_merge($at_nicknames, match_at($item['content']));
		}
		!empty($user_ids) && $user_ids = array_unique($user_ids);
		!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
		
		//匹配@的用户数据
		if (!empty($at_nicknames)) {
			$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
			$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
		}
		
		//查询用户数据
		if (!empty($user_ids)) {
			$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
			$weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
			foreach ($weibo_user_list as $weibo_user_list_k => $weibo_user_list_v) {
				$weibo_user_list[$weibo_user_list_k] = array_merge($user_list[$weibo_user_list_k], $weibo_user_list_v);
			}
		}
		
		//整合数据
		foreach ($comment_list as $k => $v){
			$comment_list[$k]['from_user_data'] = $weibo_user_list[$v['from_uid']];
			$comment_list[$k]['to_user_data'] = $weibo_user_list[$v['to_uid']];
		}
		
		$assign['list'] = $comment_list;
		$assign['at_data'] = $weibo_at_name_id_array;
		$this -> output($assign);
	}
	
	/**
	 * 获取微博的转发列表
	 * @author	Baiyu
	 * @param	number	$weibo_id		微博ID
	 * @param	number	$page			页码
	 * @param	number	$offset			起始行
	 * @param	number	$limit			取几行
	 */
	public function forwardList($weibo_id = 0, $page = 0, $offset = 0, $limit = 50){
		
		//TODO 将来实现权限机制

		//申明变量
		$error = 0;
		$error_msg = '';
		$user_ids = array();				//相关用户IDs
		$new_ids = array();					//转发成生的新微博IDs
		$forward_list = array();			//转发列表
		$weibo_user_list = array();			//相关用户数据
		$at_nicknames = array();			//@的用户名
		$weibo_at_name_id_array = array();	//at的用户名和user_id对应关系

		//分页机制 page和offset limit二选一
		$new_ids = D('WeiboForward') -> getNewidsByWeiboid($weibo_id, $page, $offset, $limit);
		!empty($new_ids) && $forward_list = D('Weibo') -> where(array('weibo_id' => array('IN', $new_ids))) -> select();

		if (!empty($forward_list)) {
			foreach ($forward_list as $item){
				$user_ids[] = $item['user_id'];
				
				//@的用户昵称
				$at_nicknames = array_merge($at_nicknames, match_at($item['content']));
			}
			!empty($user_ids) && $user_ids = array_unique($user_ids);
			!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			
			//匹配@的用户数据
			if (!empty($at_nicknames)) {
				$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
				$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
			}
			
			//获取用户数据
			if (!empty($user_ids)) {
				$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
				$weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
				foreach ($weibo_user_list as $weibo_user_list_k => $weibo_user_list_v) {
					$weibo_user_list[$weibo_user_list_k] = array_merge($user_list[$weibo_user_list_k], $weibo_user_list_v);
				}
			}
			
			//整合数据
			foreach ($forward_list as $k => $v){
				$forward_list[$k]['user_data'] = $weibo_user_list[$v['user_id']];
			}
		} else {
			$error = 102;
			$error_msg = '数据为空';
		}
		
		$assign['error'] = $error;
		$assign['error_msg'] = $error_msg;
		$assign['list'] = $forward_list;
		$assign['at_data'] = $weibo_at_name_id_array;
		$this -> output($assign);
	}
	
	/**
	 * 删除微博
	 * @author	Baiyu
	 * @param	number	$weibo_id		微博ID
	 */
	public function delete($weibo_id = 0){
		
		//申明变量
		$result = FALSE;			//处理结果
		$error = FALSE;				//错误代码
		$error_msg = '';			//错误信息
		$weibo = array();			//微博数据
		$log_array = array();		//错误日志数据
		
		//判断是否本人微博
		$weibo = D('Weibo') -> find($weibo_id);
		if (empty($weibo) || $weibo['user_id'] != $this -> userId) {
			$error = 202;
			$error_msg = '请勿非法操作';
			
		//删除微博及相关数据
		} else {
			
			//删除微博
			$result = D('Weibo') -> delete($weibo_id);
			if (!$result) {
				$log_array['step'] = '3.1';
				$log_array['err_msg'] = '删除微博出错';
			}
			
			//删除微博index_feed
			$result && $result = TRUE && D('WeiboIndexFeed') -> where(array('weibo_id' => $weibo_id)) -> delete();
			if (!$result) {
				$log_array['step'] = '3.3';
				$log_array['err_msg'] = '删除index_feed出错';
			}
			
			//删除微博附件
			//TODO 如何删除文件
			$result && $result =  FALSE !== D('WeiboAttachment') -> where(array('weibo_id' => $weibo_id)) -> delete();
			if (!$result) {
				$log_array['step'] = '3.2';
				$log_array['err_msg'] = '删除微博附件出错';
			}
			
			//删除微博评论
			$result && $result = FALSE !== D('WeiboComment') -> where(array('weibo_id' => $weibo_id)) -> delete();
			if (!$result) {
				$log_array['step'] = '3.4';
				$log_array['err_msg'] = '删除微博评论出错';
			}
			
			//如果为转发，修改原微博的forward_count
			if ($weibo['type'] == 2) {
				D('Weibo') -> where(array('weibo_id' => $weibo['original_id'])) -> save(array('forward_count' => array('exp', 'forward_count-1')));
			}
			
			//修改weibo_user的weibo_count
			$result && $result = TRUE && D('WeiboUser') -> where(array('user_id' => $this -> userId)) -> save(array('weibo_count' => array('exp', 'weibo_count-1')));
			if (!$result) {
				$log_array['step'] = '3.6';
				$log_array['err_msg'] = '修改用户微博数失败';
			}
			
			//错误处理
			if (!$result) {
				$error = 101;
				$error_msg = '操作失败';
					
				$log_array['on'] = 'delete_weibo';
				$log_array['weibo_id'] = $weibo_id;
				$log_array['uid'] = $this -> userId;
				$log_array['step'] = $log_array['step'] ?: '3';
				$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
				weibo_log($log_array);
			}
		}
		
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['status'] = intval($result);
		$this -> output($assign);
	}
	
	/**
	 * 点赞/取消赞 统一入口
	 * @author	Baiyu
	 * @param	number	id			微博或评论的ID
	 * @param	string	$type		类型weibo为赞微博|comment为赞评论
	 */
	public function praise($id = 0, $type = 'weibo') {
		switch ($type) {
			case 'weibo':
				$this -> praise_weibo($id);
				break;
			case 'comment':
				$this -> praise_comment($id);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 赞微博/取消赞
	 * @author	Baiyu
	 * @param	number	$weibo_id	微博ID
	 */
	private function praise_weibo($weibo_id = 0){

		//申明变量
		$result = FALSE;
		$error = FALSE;
		$error_msg = '';
		$type = '';
		$msg = '';
		$weibo = array();
		$praised = FALSE;
		$log_array = array();
		
		//查出微博
		$weibo = D('Weibo') -> find($weibo_id);
		if(empty($weibo)){
			$error = TRUE;
			$error_msg = '这个微博不存在';
		}
		
		//处理赞
		if (!$error) {
			
			//权限检测是否黑名单是否已停用
			$this -> checkWeiboPermission($weibo);
			
			//检测是否已赞过
			$praised = TRUE && D('WeiboPraise') -> where(array('weibo_id' => $weibo_id, 'from_uid' => $this -> userId)) -> count();
			
			//赞
			if (!$praised) {
				$type = 'praise';
				$msg = '点赞';
				
				//赞数增加
				$result = TRUE && D('Weibo') -> where(array('weibo_id' => $weibo_id)) -> save(array('praised_count' => array('exp', 'praised_count+1'), 'praise_count' => array('exp', 'praise_count+1')));
				if (!$result) {
					$log_array['step'] = '3.3.1';
					$log_array['err_msg'] = '赞数增加时出错';
				}
				
				//写入赞记录表
				$result && $result = TRUE && D('WeiboPraise') -> add(array(
						'weibo_id' => $weibo_id,
						'original_weibo_id' => $weibo['original_id'],
						'from_uid' => $this -> userId,
						'to_uid' => $weibo['user_id'],
						'create_time' => NOW_TIME,
				));
				if (!$result) {
					$log_array['step'] = '3.3.2';
					$log_array['err_msg'] = '写入赞记录表时出错';
				}
				
				//通知
				$result && push_weibo_notify($weibo['user_id'], $this -> userId, 'praise');
				
			//取消赞
			} else {
				$type = 'unpraise';
				$msg = '取消赞';
				
				//赞数减少
				$result = TRUE && D('Weibo') -> where(array('weibo_id' => $weibo_id)) -> save(array('praise_count' => array('exp', 'praise_count-1')));
				if (!$result) {
					$log_array['step'] = '3.4.1';
					$log_array['err_msg'] = '赞数减少时出错';
				}
				
				//删除赞记录表中数据
				$result && $result = D('WeiboPraise') -> where(array('weibo_id' => $weibo_id, 'from_uid' => $this -> userId)) -> delete();
				if (!$result) {
					$log_array['step'] = '3.4.2';
					$log_array['err_msg'] = '删除赞记录表中数据时出错';
				}
			}
			
			//错误处理
			if (!$result) {
				$error = 101;
				$error_msg = '操作失败';
					
				$log_array['on'] = 'praise_weibo';
				$log_array['weibo_id'] = $weibo_id;
				$log_array['uid'] = $this -> userId;
				$log_array['step'] = $log_array['step'] ?: '3';
				$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
				weibo_log($log_array);
			}
		}
		
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['status'] = intval($result);
		$assign['type'] = $type;
		$assign['msg'] = $msg . ($result ? '成功' : '失败');
		$this -> output($assign);
	}
	
	/**
	 * 赞评论/取消赞
	 * @author	Baiyu
	 * @param	number	$comment_id		评论ID
	 */
	private function praise_comment($comment_id = 0){
	
		//申明变量
		$result = FALSE;
		$error = FALSE;
		$error_msg = '';
		$type = '';
		$msg = '';
		$comment = array();
		$praised = FALSE;
		$log_array = array();
	
		//查出微博
		$comment = D('WeiboComment') -> find($comment_id);
		if(empty($comment)){
			$error = TRUE;
			$error_msg = '这条评论不存在';
		}
	
		//处理赞
		if (!$error) {
				
			//权限检测是否黑名单是否已停用
			$this -> checkWeiboPermission($comment);
				
			//检测是否已赞过
			$praised = TRUE && D('WeiboPraise') -> where(array('comment_id' => $comment_id, 'from_uid' => $this -> userId)) -> count();
				
			//赞
			if (!$praised) {
				$type = 'praise';
				$msg = '点赞';
				
				//赞数增加
				$result = TRUE && D('WeiboComment') -> where(array('comment_id' => $comment_id)) -> save(array('praise_count' => array('exp', 'praise_count+1')));
				if (!$result) {
					$log_array['step'] = '3.3.1';
					$log_array['err_msg'] = '赞数增加时出错';
				}
	
				//写入赞记录表
				$result && $result = TRUE && D('WeiboPraise') -> add(array(
						'original_weibo_id' => $comment['original_weibo_id'],
						'comment_id' => $comment_id,
						'from_uid' => $this -> userId,
						'to_uid' => $comment['from_uid'],
						'create_time' => NOW_TIME,
				));
				if (!$result) {
					$log_array['step'] = '3.3.2';
					$log_array['err_msg'] = '写入赞记录表时出错';
				}
				
				//通知
				$result && push_weibo_notify($comment['user_id'], $this -> userId, 'praise');
	
			//取消赞
			} else {
				$type = 'unpraise';
				$msg = '取消赞';
				
				//赞数减少
				$result = TRUE && D('WeiboComment') -> where(array('comment_id' => $comment_id)) -> save(array('praise_count' => array('exp', 'praise_count-1')));
				if (!$result) {
					$log_array['step'] = '3.4.1';
					$log_array['err_msg'] = '赞数减少时出错';
				}
	
				//删除赞记录表中数据
				$result && $result = D('WeiboPraise') -> where(array('comment_id' => $comment_id, 'from_uid' => $this -> userId)) -> delete();
				if (!$result) {
					$log_array['step'] = '3.4.2';
					$log_array['err_msg'] = '删除赞记录表中数据时出错';
				}
			}
	
			//错误处理
			if (!$result) {
				$error = 101;
				$error_msg = '操作失败';
					
				$log_array['on'] = 'praise_weibo';
				$log_array['comment_id'] = $comment_id;
				$log_array['uid'] = $this -> userId;
				$log_array['step'] = $log_array['step'] ?: '3';
				$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
				weibo_log($log_array);
			}
		}
	
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['type'] = $type;
		$assign['msg'] = $msg . ($result ? '成功' : '失败');
		$assign['status'] = intval($result);
		$this -> output($assign);
	}
	
	/**
	 * 评论
	 */
	public function addcomment_old($weibo_id){
		return;
		
		//权限检测是否黑名单是否已停用
		
		//申明变量
		$weibo_model = D('Weibo');
		$comment = array(
			'weibo_id' => $weibo_id,
			'original_comment_id' => 0,
			'from_uid' => $this -> userId,
			'to_uid' => 0,	//TODO
			'content' => I('post.content'),
			'praise_count' => 0,
			'create_time' => NOW_TIME,
			'page_index' => 0,
		);
		$assign = array();
		
		//TODO 匹配@和敏感词
		
		//查出微博
		$weibo = $weibo_model -> find($weibo_id);
		
		if (!empty($weibo)) {
			
			//计算当前评论所在页码
			$comment['page_index'] = ceil($weibo['commented_count'] / C('WEIBO.page_length'));
			
			//插入评论表
			$comment_id = D('WeiboComment') -> add($comment);
			
			if ($comment_id) {
				//修改微博的评论数
				$weibo_update = array(
						'comment_count' => array('exp', 'comment_count+1'),
						'commented_count' => array('exp', 'commented_count+1'),
				);
				$result = $weibo_model -> where(array('weibo_id' => $weibo_id)) -> save($weibo_update);
				
				//通知微博的作者
				push_weibo_notify($weibo['user_id'], $this -> userId, 'comment');
				
				//TODO 通知@的用户
				
				//TODO 记录风险
				
			} else {
				//错误 评论插入失败
				$assign['error'] = 101;
				$assign['error_msg'] = '系统繁忙评论失败';
			}
		} else {
			//错误 没有这条微博
			$assign['error'] = 201;
			$assign['error_msg'] = '没有这条微博无法评论';
		}
		
		$this -> output($assign);
	}
	
	/**
	 * 发表评论统一入口
	 * 暂不支持图片评论
	 * @author	Baiyu
	 * @param	number	$id			微博或评论的ID
	 * @param	string	$content	评论内容
	 * @param	string	$type		类型weibo为微博|comment为回复评论
	 */
	public function addcomment($id = 0, $content = '', $type = 'weibo') {
		switch ($type) {
			case 'weibo':
				$this -> comment_weibo($id, $content);
				break;
			case 'comment':
				$this -> reply_comment($id, $content);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 评论微博
	 * @author	Baiyu
	 * @param	number	$weibo_id	微博ID
	 * @param	string	$content	评论内容
	 */
	private function comment_weibo($weibo_id = 0, $content = ''){
		
		//申明变量
		$result = FALSE;								//处理结果
		$error = FALSE;									//错误代码
		$error_msg = '';								//错误消息
		$weibo = array();								//微博数据
		$comment = array();								//评论数据
		$comment_id = 0;								//评论ID
		$page_index = 0;								//页码
		$items_per_page = C('WEIBO.page_length');		//每页放多少条评论
		$at_user_id_name = array();						//被@的用户的ID和昵称对应关系
		$at_user_ids = array();							//要@通知的用户IDs
		$log_array = array();							//错误日志
		
		//组织参数
		$content = substr($content, 0, 140);			//最多允许140字
		
		//查出微博
		$weibo = D('Weibo') -> find($weibo_id);
		if(empty($weibo)){
			$error = TRUE;
			$error_msg = '这个微博不存在';
		}

		//匹配@
		if (!empty($content)) {
			$at_nicknames = match_at($content);
			$at_user_id_name = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
			$at_user_ids = array_keys($at_user_id_name);
		}
		
		//TODO匹配敏感词
		
		//处理评论
		if (!$error) {
			
			//插入评论
			$comment = array(
				'original_comment_id' => 0,
				'weibo_id' => $weibo_id,
				'original_weibo_id' => $weibo['original_id'],
				'from_uid' => $this -> userId,
				'to_uid' => $weibo['user_id'],
				'content' => $content,
				'praise_count' => 0,
				'create_time' => NOW_TIME,
				'page_index' => ceil($weibo['commented_count'] / $items_per_page) ?: 1,
			);
			$comment_id = D('WeiboComment') -> add($comment);
			
			//评论相关数据操作
			if ($comment_id) {
				//weibo的评论数增加
				$result = TRUE && D('Weibo') -> where(array('weibo_id' => $weibo_id)) -> save(array('commented_count' => array('exp', 'commented_count+1'), 'comment_count' => array('exp', 'comment_count+1')));
				if (!$result) {
					$log_array['step'] = '6.2.1';
					$log_array['err_msg'] = '增加微博评论数时出错';
				}
				
				//插入at表(at有可能转发时at也有可以原创微博中就at,所以不放到转发处理中)
				foreach ($at_user_id_name as $k => $v) {
					$ats[] = array(
							'weibo_id' => $weibo_id,
							'original_weibo_id' => $weibo['original_id'],
							'comment_id' => $comment_id,
							'original_comment_id' => 0,
							'type' => 2,
							'from_uid' => $this -> userId,
							'to_uid' => $k,
							'to_nickname' => $v,
							'create_time' => NOW_TIME,
					);
				}
				$result && !empty($ats) && $result = TRUE && D('WeiboAt') -> addAll($ats);
				if (!$result) {
					$log_array['step'] = '6.2.2';
					$log_array['err_msg'] = '插入@表时出错';
				}
				
				//通知@用户
				$result && !empty($ats) && $result = TRUE && push_weibo_notify($at_user_ids, $this -> userId, 'at');
				if (!$result) {
					$log_array['step'] = '6.2.3';
					$log_array['err_msg'] = '通知@用户时出错';
				}
				
				//通知微博作者
				$result && $result = TRUE && push_weibo_notify($weibo['user_id'], $this -> userId, 'comment');
				if (!$result) {
					$log_array['step'] = '6.2.4';
					$log_array['err_msg'] = '通知微博作者时出错';
				}
				
				//错误处理
				if (!$result) {
					$error = 101;
					$error_msg = '操作失败';
						
					$log_array['on'] = 'comment_weibo';
					$log_array['weibo_id'] = $weibo_id;
					$log_array['uid'] = $this -> userId;
					$log_array['step'] = $log_array['step'] ?: '6.2';
					$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
					weibo_log($log_array);
				}
			}
		}
		
		//输出
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['status'] = intval($result);
		$this -> output($assign);
	}
	

	/**
	 * 回复评论
	 * @author	Baiyu
	 * @param	number	$original_comment_id	原评论ID
	 * @param	string	$content				评论内容
	 */
	private function reply_comment($original_comment_id = 0, $content = ''){
	
		//申明变量
		$result = FALSE;								//处理结果
		$error = FALSE;									//错误代码
		$error_msg = '';								//错误消息
		$original_comment = array();					//原评论数据
		$weibo = array();								//微博数据
		$comment = array();								//评论数据
		$comment_id = 0;								//评论ID
		$page_index = 0;								//页码
		$items_per_page = C('WEIBO.page_length');		//每页放多少条评论
		$at_user_id_name = array();						//被@的用户的ID和昵称对应关系
		$at_user_ids = array();							//要@通知的用户IDs
		$notify_comment_user_ids = array();				//被评论的用户IDs
		$log_array = array();							//错误日志
		
		//组织参数
		$content = substr($content, 0, 140);			//最多允许140字
		
		//查出评论
		$original_comment = D('WeiboComment') -> find($original_comment_id);
		if (empty($original_comment)){
			$error = TRUE;
			$error_msg = '这条评论不存在';
		}
		$weibo = D('Weibo') -> find($original_comment['weibo_id']);
		if (empty($weibo)) {
			$error = TRUE;
			$error_msg = '微博不存在';
		}
	
		//匹配@ 组织通知用户ID
		if (!empty($content)) {
			$at_nicknames = match_at($content);
			$at_user_id_name = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
			$at_user_ids = array_keys($at_user_id_name);
		}
		$notify_comment_user_ids = array($weibo['user_id'], $original_comment['from_uid']);
	
		//TODO匹配敏感词
	
		//处理评论
		if (!$error) {
				
			//插入评论
			$comment = array(
					'original_comment_id' => $original_comment_id,
					'weibo_id' => $original_comment['weibo_id'],
					'original_weibo_id' => $original_comment['original_weibo_id'],
					'from_uid' => $this -> userId,
					'to_uid' => $original_comment['from_uid'],		//TODO 原微博算不算被评论
					'content' => $content,
					'praise_count' => 0,
					'create_time' => NOW_TIME,
					'page_index' => ceil($original_comment['commented_count'] / $items_per_page) ?: 1,
			);
			$comment_id = D('WeiboComment') -> add($comment);
				
			//评论相关数据操作
			if ($comment_id) {
				
				//weibo的评论数增加
				$result = TRUE && D('Weibo') -> where(array('weibo_id' => $original_comment['weibo_id'])) -> save(array('commented_count' => array('exp', 'commented_count+1'), 'comment_count' => array('exp', 'comment_count+1')));
				if (!$result) {
					$log_array['step'] = '6.2.1';
					$log_array['err_msg'] = '增加微博评论数时出错';
				}
				
				//插入at表(at有可能转发时at也有可以原创微博中就at,所以不放到转发处理中)
				foreach ($at_user_id_name as $k => $v) {
					$ats[] = array(
							'weibo_id' => $original_comment['weibo_id'],
							'original_weibo_id' => $weibo['original_id'],
							'comment_id' => $comment_id,
							'original_comment_id' => $original_comment_id,
							'type' => 2,
							'from_uid' => $this -> userId,
							'to_uid' => $k,
							'to_nickname' => $v,
							'create_time' => NOW_TIME,
					);
				}
				$result && !empty($ats) && $result = TRUE && D('WeiboAt') -> addAll($ats);
				if (!$result) {
					$log_array['step'] = '6.2.2';
					$log_array['err_msg'] = '插入@表时出错';
				}
				
				//通知@用户
				$result = TRUE && push_weibo_notify($at_user_ids, $this -> userId, 'at');
				if (!$result) {
					$log_array['step'] = '6.2.3';
					$log_array['err_msg'] = '通知@用户时出错';
				}
	
				//通知评论和微博作者
				$result && $result = TRUE && push_weibo_notify($notify_comment_user_ids, $this -> userId, 'comment');
				if (!$result) {
					$log_array['step'] = '6.2.4';
					$log_array['err_msg'] = '通知微博和评论作者时出错';
				}
	
				//错误处理
				if (!$result) {
					$error = 101;
					$error_msg = '操作失败';
	
					$log_array['on'] = 'reply_comment';
					$log_array['weibo_id'] = $weibo['weibo_id'];
					$log_array['original_comment_id'] = $original_comment_id;
					$log_array['uid'] = $this -> userId;
					$log_array['step'] = $log_array['step'] ?: '6.2';
					$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
					weibo_log($log_array);
				}
			}
		}
	
		//输出
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['status'] = intval($result);
		$this -> output($assign);
	}
	
	/**
	 * 删除评论
	 * @author	Baiyu
	 * @param	number	$comment_id		评论ID
	 */
	public function delcomment($comment_id = 0){

		//申明变量
		$result = FALSE;			//处理结果
		$error = FALSE;				//错误代码
		$error_msg = '';			//错误信息
		$comment = array();			//评论数据
		$log_array = array();		//错误日志数据
		
		//判断是否为评论作者或微博作者
		$comment = D('WeiboComment') -> find($comment_id);
		if (empty($comment) || ($comment['to_uid'] != $this -> userId && $comment['from_uid'] != $this -> userId)) {
			$error = 202;
			$error_msg = '请勿非法操作';
		}
		
		//删除评论和处理相关
		if (!$error) {
			//删除评论
			$result = D('WeiboComment') -> delete($comment_id);
			if (!$result) {
				$log_array['step'] = '3.1';
				$log_array['err_msg'] = '删除评论时出错';
			}
			
			//减少微博的comment_count
			$result && $result = TRUE && D('Weibo') -> where(array('weibo_id' => $comment['weibo_id'])) -> save(array('comment_count' => array('exp', 'comment_count-1')));
			if (!$result) {
				$log_array['step'] = '3.2';
				$log_array['err_msg'] = '删除评论时出错';
			}
			
			//错误处理
			if (!$result) {
				$error = 101;
				$error_msg = '操作失败';
			
				$log_array['on'] = 'delcomment';
				$log_array['comment_id'] = $comment_id;
				$log_array['weibo_id'] = $comment['weibo_id'];
				$log_array['uid'] = $this -> userId;
				$log_array['step'] = $log_array['step'] ?: '3';
				$log_array['err_msg'] = $log_array['err_msg'] ?: '操作失败';
				weibo_log($log_array);
			}
		}
		
		//输出
		$assign['error'] = intval($error);
		$assign['error_msg'] = $error_msg;
		$assign['status'] = intval($result);
		$this -> output($assign);
	}
	
	/**
	 * at我的列表
	 * @param number $limit		一次刷新几条
	 * @param string $time		时间区间或起始时间，如果为时间区间则用逗号隔开
	 */
	public function atMe($limit = 0, $time = ''){
		
		//申明变量
		$error = 0;
		$error_msg = '';
		$time_begin = 0;
		$time_end = 0;
		$list = array();
		$user_ids = array();
		$user_list = array();
		$weibo_user_list = array();
		$weibo_ids = array();
		$weibo_list = array();
		$comment_ids = array();
		$comment_list = array();
		$attachments = array();
		$at_nicknames = array();
		$weibo_at_name_id_array = array();
		
		$limit = $limit ?: 20;
		$where = array(
			'to_uid' => $this -> userId,
		);
		
		//处理参数-时间区间
		if ($time) {
			if (strpos($time, ',') !== FALSE) {
				list($time_begin, $time_end) = explode(',', $time);
			} else {
				$time_begin = $time;
			}
		
			empty($time_end) && $time_end = NOW_TIME;
			$where['create_time'] = array('BETWEEN', array(intval($time_begin), intval($time_end)));
		}
		
		//取数据
		$list = D('WeiboAt') -> where($where) -> limit(0, $limit) -> select();
		
		//取出关联数据
		if (!empty($list)) {
			//取出关联数据的ID(现在还不全)
			foreach ($list as $at) {
				$weibo_ids[] = $at['weibo_id'];
				$at['original_weibo_id'] && $weibo_ids[] = $at['original_weibo_id'];
				
				$at['comment_id'] && $comment_ids[] = $at['comment_id'];
				$at['original_comment_id'] && $comment_ids[] = $at['original_comment_id'];
				$at['from_uid'] && $user_ids[] = $at['from_uid'];
			}
			
			//拿comment_ids取comments,并循环得到weibo_ids
			!empty($comment_ids) && $comment_list = D('WeiboComment') -> where(array('comment_id' => array('IN', $comment_ids))) -> select();
			if (!empty($comment_list)) {
				$comment_list = field_as_key($comment_list, 'comment_id');
				foreach ($comment_list as $comment) {
					$comment['weibo_id'] && $weibo_ids[] = $comment['weibo_id'];
				}
			}
			$weibo_ids = array_unique($weibo_ids);
			
			//取微博数据,取出微博数据中的user_id,取附件并整合
			!empty($weibo_ids) && $weibo_list = D('Weibo') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
			if (!empty($weibo_list)) {
				//取微博
				$weibo_list = field_as_key($weibo_list, 'weibo_id');
				
				//取附件数据
				$attachments = D('WeiboAttachment') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
				$attachments = field_as_key($attachments, 'weibo_id', TRUE);
				
				//取微博中的user_id,匹配微博中的@用户名,并整合附件
				foreach ($weibo_list as $weibo_k => $weibo) {
					$weibo_list[$weibo_k]['attach'] = isset($attachments[$weibo['weibo_id']]) ? $attachments[$weibo['weibo_id']] : array();
					$weibo['user_id'] && $user_ids[] = $weibo['user_id'];
					
					//@的用户昵称
					$at_nicknames = array_merge($at_nicknames, match_at($weibo['content']));
				}
				!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			}
			$user_ids = array_unique($user_ids);
			
			//匹配@的用户数据
			if (!empty($at_nicknames)) {
				$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
				$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
			}
			
			//取用户信息
			!empty($user_ids) && $weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
			if (!empty($weibo_user_list)) {
				$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
				foreach ($weibo_user_list as $weibo_user_list_k => $weibo_user_list_v) {
					$weibo_user_list[$weibo_user_list_k] = array_merge($user_list[$weibo_user_list_k], $weibo_user_list_v);
				}
			}
			
			//整合数据
			foreach ($list as $k => $v) {
				//@列表的内容就是1+2 或 2+3
				
				//格式化时间
				$list[$k]['time_str'] = time2str($v['create_time']);
				
				//用户数据
				$list[$k]['user_data'] = $weibo_user_list[$v['from_uid']];
				
				//评论数据
				if ($v['comment_id']) {
					$comment_list[$v['comment_id']]['user_data'] = $weibo_user_list[$comment_list[$v['comment_id']]['from_uid']];
					$list[$k]['comment'] = $comment_list[$v['comment_id']];
				} else {
					$list[$k]['comment'] = array();
				}
				
				//原始评论数据
				if ($v['original_comment_id']) {
					$comment_list[$v['original_comment_id']]['user_data'] = $weibo_user_list[$comment_list[$v['original_comment_id']]['from_uid']];
					$list[$k]['original_comment'] = $comment_list[$v['original_comment_id']];
				} else {
					$list[$k]['original_comment'] = array();
				}
				
				//微博数据
				$weibo_list[$v['weibo_id']]['user_data'] = $weibo_user_list[$weibo_list[$v['weibo_id']]['user_id']];
				$list[$k]['weibo'] = $weibo_list[$v['weibo_id']];
				
				//原始微博数据
				if ($v['original_weibo_id']) {
					$weibo_list[$v['original_weibo_id']]['user_data'] = $weibo_user_list[$weibo_list[$v['original_weibo_id']]['user_id']];
					$list[$k]['original_weibo'] = $weibo_list[$v['original_weibo_id']];
				} else {
					$list[$k]['original_weibo'] = array();
				}
			}
			
			
		//错误处理
		} else {
			if (FALSE === $list) {
				$error = 101;
				$error_msg = '操作失败';
			} elseif (empty($list)) {
				$error = 102;
				$error_msg = '没有数据';
			} else {
					
			}
		}
		
		//输出结果
		$assign['error'] = $error;
		$assign['error_msg'] = $error_msg;
		$assign['list'] = $list;
		$assign['at_data'] = $weibo_at_name_id_array;
		
		$this -> output($assign);
	}
	
	/**
	 * 评论我的列表
	 * @param unknown $limit
	 * @param string $time
	 */
	public function commentAboutMe($limit = 0, $time = ''){
		
		//申明变量
		$error = 0;
		$error_msg = '';
		$time_begin = 0;
		$time_end = 0;
		$list = array();
		$user_ids = array();
		$user_list = array();
		$weibo_user_list = array();
		$weibo_ids = array();
		$weibo_list = array();
		$original_comment_ids = array();
		$original_comment_list = array();
		$attachments = array();
		$at_nicknames = array();
		$weibo_at_name_id_array = array();
		
		$limit = $limit ?: 20;
		$where = array(
			'to_uid' => $this -> userId,
		);
		
		//处理参数-时间区间
		if ($time) {
			if (strpos($time, ',') !== FALSE) {
				list($time_begin, $time_end) = explode(',', $time);
			} else {
				$time_begin = $time;
			}
		
			empty($time_end) && $time_end = NOW_TIME;
			$where['create_time'] = array('BETWEEN', array(intval($time_begin), intval($time_end)));
		}
		
		//取数据
		$list = D('WeiboComment') -> where($where) -> limit(0, $limit) -> select();
		
		//取关联数据
		if (!empty($list)) {
			//记录中取出关联ID
			foreach ($list as $comment) {
				//user_ids, comment_ids, weibo_ids
				$weibo_ids[] = $comment['weibo_id'];
				$comment['original_weibo_id'] && $weibo_ids[] = $comment['original_weibo_id'];
				$comment['original_comment_id'] && $original_comment_ids[] = $comment['original_comment_id'];
				$user_ids[] = $comment['from_uid'];
				$user_ids[] = $comment['to_uid'];
				//@的用户昵称
				$at_nicknames = array_merge($at_nicknames, match_at($comment['content']));
			}
			!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			
			//取原始评论 并从中取出 user_id
			!empty($original_comment_ids) && $original_comment_list = D('WeiboComment') -> where(array('comment_id' => array('IN', $original_comment_ids))) -> select();
			if (!empty($original_comment_list)) {
				$original_comment_list = field_as_key($original_comment_list, 'comment_id');
				foreach ($original_comment_list as $original_comment) {
					$original_comment['to_uid'] && $user_ids[] = $original_comment['to_uid'];
					//@的用户昵称
					$at_nicknames = array_merge($at_nicknames, match_at($original_comment['content']));
				}
				!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			}
			
			//取微博数据,取出微博数据中的user_id,取附件并整合
			$weibo_ids = array_unique($weibo_ids);
			!empty($weibo_ids) && $weibo_list = D('Weibo') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
			if (!empty($weibo_list)) {
				//取微博
				$weibo_list = field_as_key($weibo_list, 'weibo_id');
				
				//取附件数据
				$attachments = D('WeiboAttachment') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
				$attachments = field_as_key($attachments, 'weibo_id', TRUE);
				
				//取微博中的user_id并整合附件
				foreach ($weibo_list as $weibo_k => $weibo) {
					$weibo_list[$weibo_k]['attach'] = isset($attachments[$weibo['weibo_id']]) ? $attachments[$weibo['weibo_id']] : array();
					$weibo['user_id'] && $user_ids[] = $weibo['user_id'];
				}
			}
			
			//匹配@的用户数据
			if (!empty($at_nicknames)) {
				$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
				$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
			}
			
			//取用户信息
			$user_ids = array_unique($user_ids);
			!empty($user_ids) && $weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
			if (!empty($weibo_user_list)) {
				$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
				foreach ($weibo_user_list as $weibo_user_list_k => $weibo_user_list_v) {
					$weibo_user_list[$weibo_user_list_k] = array_merge($user_list[$weibo_user_list_k], $weibo_user_list_v);
				}
			}
			
			//整合数据
			foreach ($list as $k => $v) {
				
				//格式化时间
				$list[$k]['time_str'] = time2str($v['create_time']);
				
				//用户数据
				$list[$k]['user_data'] = $weibo_user_list[$v['from_uid']];
				
				//原始评论数据
				if ($v['original_comment_id']) {
					$original_comment_list[$v['original_comment_id']]['user_data'] = $weibo_user_list[$original_comment_list[$v['original_comment_id']]['from_uid']];
					$list[$k]['original_comment'] = $original_comment_list[$v['original_comment_id']];
				} else {
					$list[$k]['original_comment'] = array();
				}
				
				//微博数据
				$weibo_list[$v['weibo_id']]['user_data'] = $weibo_user_list[$weibo_list[$v['weibo_id']]['user_id']];
				$list[$k]['weibo'] = $weibo_list[$v['weibo_id']];
				
				//原始微博数据
				if ($v['original_weibo_id']) {
					$weibo_list[$v['original_weibo_id']]['user_data'] = $weibo_user_list[$weibo_list[$v['original_weibo_id']]['user_id']];
					$list[$k]['original_weibo'] = $weibo_list[$v['original_weibo_id']];
				} else {
					$list[$k]['original_weibo'] = array();
				}
			}
			
		//错误处理
		} else {
			if (FALSE === $list) {
				$error = 101;
				$error_msg = '操作失败';
			} elseif (empty($list)) {
				$error = 102;
				$error_msg = '没有数据';
			} else {
					
			}
		}
		
		//输出结果
		$assign['error'] = $error;
		$assign['error_msg'] = $error_msg;
		$assign['list'] = $list;
		$assign['at_data'] = $weibo_at_name_id_array;
		
		$this -> output($assign);
	}
	
	public function praiseAboutMe($limit = 0, $time = ''){
		
		//申明变量
		$error = 0;
		$error_msg = '';
		$time_begin = 0;
		$time_end = 0;
		$list = array();
		$user_ids = array();
		$user_list = array();
		$weibo_user_list = array();
		$weibo_ids = array();
		$weibo_list = array();
		$comment_ids = array();
		$comment_list = array();
		$attachments = array();
		$at_nicknames = array();
		$weibo_at_name_id_array = array();
		
		$limit = $limit ?: 20;
		$where = array(
			'to_uid' => $this -> userId,
		);
		
		//处理参数-时间区间
		if ($time) {
			if (strpos($time, ',') !== FALSE) {
				list($time_begin, $time_end) = explode(',', $time);
			} else {
				$time_begin = $time;
			}
		
			empty($time_end) && $time_end = NOW_TIME;
			$where['create_time'] = array('BETWEEN', array(intval($time_begin), intval($time_end)));
		}
		
		//取数据
		$list = D('WeiboPraise') -> where($where) -> limit(0, $limit) -> select();
		
		//取关联数据
		if (!empty($list)) {
			//记录中取出关联ID
			foreach ($list as $praise) {
				//user_ids, comment_ids, weibo_ids
				$weibo_ids[] = $praise['weibo_id'];
				$praise['original_weibo_id'] && $weibo_ids[] = $praise['original_weibo_id'];
				$praise['comment_id'] && $comment_ids[] = $praise['comment_id'];
				$user_ids[] = $praise['from_uid'];
				$user_ids[] = $praise['to_uid'];
			}
			
			//取评论 并从中取出 user_id
			!empty($comment_ids) && $comment_list = D('WeiboComment') -> where(array('comment_id' => array('IN', $comment_ids))) -> select();
			if (!empty($comment_list)) {
				$comment_list = field_as_key($comment_list, 'comment_id');
				foreach ($comment_list as $comment) {
					$comment['to_uid'] && $user_ids[] = $comment['to_uid'];
					//@的用户昵称
					$at_nicknames = array_merge($at_nicknames, match_at($comment['content']));
				}
				!empty($at_nicknames) && $at_nicknames = array_unique($at_nicknames);
			}
			
			//取微博数据,取出微博数据中的user_id,取附件并整合
			$weibo_ids = array_unique($weibo_ids);
			!empty($weibo_ids) && $weibo_list = D('Weibo') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
			if (!empty($weibo_list)) {
				//取微博
				$weibo_list = field_as_key($weibo_list, 'weibo_id');
				
				//取附件数据
				$attachments = D('WeiboAttachment') -> where(array('weibo_id' => array('IN', $weibo_ids))) -> select();
				$attachments = field_as_key($attachments, 'weibo_id', TRUE);
				
				//取微博中的user_id并整合附件
				foreach ($weibo_list as $weibo_k => $weibo) {
					$weibo_list[$weibo_k]['attach'] = isset($attachments[$weibo['weibo_id']]) ? $attachments[$weibo['weibo_id']] : array();
					$weibo['user_id'] && $user_ids[] = $weibo['user_id'];
				}
			}
			
			//匹配@的用户数据
			if (!empty($at_nicknames)) {
				$weibo_at_name_id_array = D('WeiboUser') -> getUseridByNicknames($at_nicknames, TRUE);
				$weibo_at_name_id_array = array_flip($weibo_at_name_id_array);
			}
			
			//取用户信息
			$user_ids = array_unique($user_ids);
			!empty($user_ids) && $weibo_user_list = field_as_key(D('WeiboUser') -> getUserByIds($user_ids), 'user_id');
			if (!empty($weibo_user_list)) {
				$user_list = field_as_key(translate_field('User', \User\Lib\ImiLib::getUserByIds($user_ids)), 'user_id');
				foreach ($weibo_user_list as $weibo_user_list_k => $weibo_user_list_v) {
					$weibo_user_list[$weibo_user_list_k] = array_merge($user_list[$weibo_user_list_k], $weibo_user_list_v);
				}
			}
			
			//整合数据
			foreach ($list as $k => $v) {
				
				//格式化时间
				$list[$k]['time_str'] = time2str($v['create_time']);
				
				//用户数据
				$list[$k]['user_data'] = $weibo_user_list[$v['from_uid']];
				
				//评论数据
				if ($v['comment_id']) {
					$comment_list[$v['comment_id']]['user_data'] = $weibo_user_list[$comment_list[$v['comment_id']]['from_uid']];
					$list[$k]['comment'] = $comment_list[$v['comment_id']];
				} else {
					$list[$k]['comment'] = array();
				}
				
				//微博数据
				$weibo_list[$v['weibo_id']]['user_data'] = $weibo_user_list[$weibo_list[$v['weibo_id']]['user_id']];
				$list[$k]['weibo'] = $weibo_list[$v['weibo_id']];
				
				//原始微博数据
				if ($v['original_weibo_id']) {
					$weibo_list[$v['original_weibo_id']]['user_data'] = $weibo_user_list[$weibo_list[$v['original_weibo_id']]['user_id']];
					$list[$k]['original_weibo'] = $weibo_list[$v['original_weibo_id']];
				} else {
					$list[$k]['original_weibo'] = array();
				}
			}
				
		//错误处理
		} else {
			if (FALSE === $list) {
				$error = 101;
				$error_msg = '操作失败';
			} elseif (empty($list)) {
				$error = 102;
				$error_msg = '没有数据';
			} else {
					
			}
		}
		
		//输出结果
		$assign['error'] = $error;
		$assign['error_msg'] = $error_msg;
		$assign['list'] = $list;
		$assign['at_data'] = $weibo_at_name_id_array;
		
		$this -> output($assign);
	}
	
	
}